package models

import (
	"github.com/jinzhu/gorm"
	"golang.org/x/crypto/bcrypt"
)

type Admin struct {
	gorm.Model
	UserName  string  `gorm:"type:varchar(100);unique_index;not null;comment:'用户名';"`
	Password  string  `gorm:"type:varchar(100);not null;comment:'密码';"`
	Nickname  string  `gorm:"type:varchar(50);comment:'昵称';"`
	Status    int     `gorm:"type:tinyint(1);default:0;not null;comment:'状态';"`
}


func (admin *Admin) SetPassword(password string) error {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), PassWordCost)
	if err !=nil {
		 return err
	}
	admin.Password=string(bytes)
	return nil
}


func (admin *Admin)CheckPassword(password string) bool{
	err := bcrypt.CompareHashAndPassword([]byte(admin.Password), []byte(password))
	return err == nil
}